package org.thesis.dataAccess;

import java.util.ArrayList;
import java.util.List;

import org.thesis.doituong.templates_sub;

import org.thesis.R;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class db_sub_templates
{
	final static String DB_NAME = "SUB_TEMPLATES";
	final static String DB_TABLE = "TEMPLATES";
	final static int VERSION = 1;
	final static String STT = "_stt";
	final static String TAG = "_tag";
	final static String MESSAGE = "_message";
	final static String[] OUTPUT = new String[]{STT,MESSAGE,TAG};
	final static String TAG_SUB = "sub";
	SQLiteDatabase db;
	Dbhelper dbhelper;
	String sql;
	Context context;
	String tag = "sub_templates";
	
	
	
	public db_sub_templates(Context context) {
		super();
		this.context = context;
	}



	public class Dbhelper extends SQLiteOpenHelper
	{

		public Dbhelper(Context context, String name, CursorFactory factory,
				int version) {
			super(context, name, factory, version);
			// TODO Auto-generated constructor stub
		}
		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			sql = "CREATE TABLE TEMPLATES (_id integer primary key autoincrement, "
					+ "_stt integer, _message text, _tag text);";
			db.execSQL(sql);
			Log.v(tag, "khoi tao table " + DB_TABLE);
			
		}
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			sql = "drop table if exists "+DB_TABLE;
			db.execSQL(sql);
			Log.v(tag, "drop table " + DB_TABLE);
		}
		
	} //end class dbhelper
	
	public db_sub_templates open()
	{
		dbhelper = new Dbhelper(context, DB_NAME, null, VERSION);
		db = dbhelper.getWritableDatabase();
		Log.v(tag, "Open DB sub");
		return this;
	}
	public void close()
	{
		dbhelper.close();
		Log.v(tag,"Close Db sub");
	}
	
	public long insert(templates_sub sub)
	{
		long rows = 0;
		try
		{
			ContentValues cv = new ContentValues();
			cv.put(STT, sub.getId());
			cv.put(MESSAGE, sub.getMessage());
			cv.put(TAG, sub.getTag());
			rows = db.insert(DB_TABLE, null, cv);
			Log.v(TAG, "insert thanh cong message :" + sub.getMessage());
		}catch (Exception e) {
			// TODO: handle exception
			Log.v(tag, "Loi insert "+e.toString());
		}
		return rows;
	}
	
	public Cursor query()
	{
		Cursor mCursor = null;
		try {
			mCursor = db.query(DB_TABLE, OUTPUT, null, null, null, null, null);
			
		} catch (Exception e) {
			// TODO: handle exception
			Log.v(tag,"Loi query all "+e.toString());
		}
		return mCursor;
	}
	
	public Cursor query(int stt)
	{
		Cursor mCursor = null;
		try {
			String selected = STT + " = " + stt;
			mCursor = db.query(true, DB_TABLE, OUTPUT, selected, null, null, null, null, null);
		} catch (Exception e) {
			// TODO: handle exception
			Log.v(tag,"Loi query stt "+e.toString());
		}
		return mCursor;
	}
	
	public Cursor query(String tag)//search theo tag
	{
		Cursor mCursor = null;
		try {
			String selected = TAG + " = " + "'" + tag + "'";
			mCursor = db.query(true, DB_TABLE, OUTPUT, selected, null, null, null, null, null);
		} catch (Exception e) {
			// TODO: handle exception
			Log.v(tag,"Loi query tag "+e.toString());
		}
		return mCursor;
	}
	
	public void delete(String mess)
	{
		try {
			String selected = MESSAGE + " = " + "'" + mess + "'";
			db.delete(DB_TABLE, selected, null);
			Log.v(tag, "delete thanh cong " + mess);
		} catch (Exception e) {
			// TODO: handle exception
			Log.v(tag, "loi delete message" + e.toString());
		}
	}
	
	public int insertDB()
	{
		int count = 0;
		List<String>list_mess = new ArrayList<String>();
		list_mess.add(context.getResources().getString(R.string.sub1));
		list_mess.add(context.getResources().getString(R.string.sub2));
		list_mess.add(context.getResources().getString(R.string.sub3));
		list_mess.add(context.getResources().getString(R.string.sub4));
		list_mess.add(context.getResources().getString(R.string.sub5));
		list_mess.add(context.getResources().getString(R.string.sub6));
		list_mess.add(context.getResources().getString(R.string.sub7));
		list_mess.add(context.getResources().getString(R.string.sub8));
		list_mess.add(context.getResources().getString(R.string.sub9));
		list_mess.add(context.getResources().getString(R.string.sub10));
		try {
			for(String mess : list_mess)
			{
				count ++;
				insert(new templates_sub(count,mess,TAG_SUB));
			}
			Log.v(tag, "insert db thanh cong ...");
		} catch (Exception e) {
			// TODO: handle exception
			Log.v(tag, "Loi insert db " +e.toString());
		}
		return count;
	}
}
